from rest_framework.serializers import ModelSerializer
from . import models
from rest_framework import serializers


class HeadlineTextSerializer(ModelSerializer):
    classify_id = serializers.PrimaryKeyRelatedField(label="类别ID", required=False, allow_null=True,
                                                     queryset=models.HeadlineClassify.objects.all())
    tag_id = serializers.PrimaryKeyRelatedField(label="标签ID", required=False, allow_null=True,
                                                queryset=models.HeadlineTag.objects.all())

    class Meta:
        model = models.HeadlineText
        fields = "__all__"

    def validate(self, attrs):
        return attrs

    def create(self, validated_data):
        h_l = super().create(validated_data)
        h_l.save()
        return h_l


class HeadlineDetailSerializer(ModelSerializer):
    class Meta:
        model = models.HeadlineText
        fields = "__all__"


class HeadlineDetaiCommentlSerializer(ModelSerializer):
    id = serializers.IntegerField(label="评论ID", required=False)
    parent_id = serializers.PrimaryKeyRelatedField(label="父评论ID", required=False,
                                                   queryset=models.HeadlineComment.objects.all())
    hl_id = serializers.PrimaryKeyRelatedField(label="头条ID", required=False, queryset=models.HeadlineText.objects.all())

    class Meta:
        model = models.HeadlineComment
        fields = "__all__"
